<?php
namespace App\Http\Controllers\User;

use App\Models\ClassHour;
use App\Models\User;
use App\Models\Semester;
use Illuminate\Http\Request;
use App\Http\Common\BaseController;
use function PHPUnit\Framework\isEmpty;

class LearningTimeController extends BaseController
{
    /**
     * 学时排名
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function learningTimeRank(Request $request){
        //获取当周的周数
        $week = $this->getNewWeeks();
        $semester = $this->getNewSemester();
        //用户id
        $uid = $request->userId;
        //获取用户时长等信息的同时由大到小排序(排名
        if (empty($data = ClassHour::where('weeks','=',$week)->orderBy('time_length','desc')->get(['user_id','time_length'])->toArray())) {
            return $this->returnJson(200,'成功');
        }
        //获取达标时间
        $passTime = Semester::where('id','=',$semester)->pluck('week_length')[0];
        $passUser = [];
        foreach ($data as $k => $v){
            $username = User::where('id','=',$v['user_id'])->first('username');
            if (!empty($username['username'])) {
                $v['username'] = $username['username'];
                $v['time_length'] >= $passTime?$v['isPass'] = 1: $v['isPass'] = 0;
                $v['passTime'] = $passTime;
                array_push($passUser,$v);
            }
        }

        return $this->returnJson(200,'排名成功',$passUser);

    }

    /**
     * 开始计时
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function startTime(Request $request){
        $uid = $request->userId;
        $startTime = $request->get('time');
        $week = $this->getNewWeeks();
        if (ClassHour::where('user_id','=',$uid)->where('weeks','=',$week)->update(['record_time' => $startTime])) {
            return $this->returnJson(200,'开始计时','');
        }
    }

    /**
     * 获取开始计时的时间
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function pullRecordTime(Request $request){
        $uid = $request->userId;
        $week = $this->getNewWeeks();
        $startTime = ClassHour::where('user_id','=',$uid)->where('weeks','=',$week)->first('record_time');
        return $this->returnJson(200,'获取成功',$startTime);
    }

    /**
     * 学时停止当天计时
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function endTime(Request $request){
        $uid = $request->userId;
        $endTime = $request->get('time');
        $week = $this->getNewWeeks();
        //加上当周之前的时间
        $dbTime = ClassHour::where('weeks','=',$week)->where('user_id','=',$uid)->get('time_length');
        if (count($dbTime)){
            $dbTime = ClassHour::where('weeks','=',$week)->where('user_id','=',$uid)->pluck('time_length')[0];
            $endTime = $endTime + $dbTime;
            if (!ClassHour::where('user_id','=',$uid)->where('weeks','=',$week)->update(['time_length'=>$endTime,'record_time'=>''])) {
                return $this->returnJson('停止计时失败');
            }
            return $this->returnJson('成功停止计时');
        }
        $insertData = [
            'user_id'       => $uid,
            'weeks'         => $week,
            'time_length'   => $endTime,
            'create_time'   => time()
        ];

        if (!ClassHour::insert($insertData)) {
            return $this->returnJson('停止计时失败');
        }
        return $this->returnJson('成功停止计时');
    }

    /**
     * 学时排名上方用户信息
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function userInfo(Request $request){
        $week = $this->getNewWeeks();
        $semester = $this->getNewSemester();
        //用户id
        $uid = $request->userId;

        //获取用户时长等信息的同时由大到小排序(排名
        if (empty($data = ClassHour::where('weeks','=',$week)->orderBy('time_length','desc')->get(['user_id','time_length'])->toArray())) {
            $data['username'] = User::where('id','=',$uid)->first('username')['username'];
            $data['passTime'] = Semester::where('id','=',$semester)->pluck('week_length')[0];
            $data['isPass'] = 0;
            $data['time_length'] = '';
            $data['rank'] = '';
            return $this->returnJson('200','获取个人信息成功',$data);
        }

        $passTime = Semester::where('id','=',$semester)->pluck('week_length')[0];

        $information = [];

        $rank = 0;
        foreach ($data as $key => $value) {
            if ($uid != $value['user_id']){
                $rank++;
                continue;
            }
            $rank++;

            $value['time_length'] >= $passTime ? $information['isPass'] = 1 : $information['isPass'] = 0;
            $information['passTime'] = $passTime;
            if (!count(ClassHour::where('user_id', '=', $uid)->where('weeks','=',$week)->get('time_length'))) {
                $information['time_length'] = 0;
            } else {
                $information['time_length'] = ClassHour::where('user_id', '=', $uid)->where('weeks','=',$week)->pluck('time_length')[0];
            }

            $information['rank'] = $rank;

        }

        $information['username'] = User::where('id','=',$uid)->pluck('username')[0];

        if (empty($information)){
            $information['passTime'] = Semester::where('id','=',$semester)->pluck('week_length')[0];
            $information['isPass'] = 0;
            $information['time_length'] = '';
            $information['rank'] = '';
        }
        return $this->returnJson(200,'获取用户信息成功',$information);
    }
}
